package com.link.base.base.activity.controller;

import com.alibaba.fastjson.JSONObject;
import com.link.base.base.activity.model.ActivityResult;
import com.link.base.base.activity.service.ActivityResultService;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 活动结果
 *
 * @author unknwon
 * @version 1.0
 * @date: 2018/11/8 9:22
 */

@Controller
@RequestMapping("/link/activityResult")
public class ActivityResultController extends BasicController<ActivityResult> {

    @Resource
    private ActivityResultService activityResultService;

    @Override
    public BasicService<ActivityResult> getBasicService() throws Exception {
        return activityResultService;
    }

    /**
     * 查询全部
     *
     * @param record
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/queryAll", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryAll(@JsonParam ActivityResult record, HttpSession session, HttpServletRequest request,
                                        HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            List<ActivityResult> lines = activityResultService.queryAll(record);
            List<JSONObject> jsonObjects = new ArrayList<>();
            String lastSession = "";
            String title = "";
            JSONObject jsonObjdata = null;
            int n = 0;
            for (ActivityResult line : lines) {
                if ("".equals(title)) {
                    title = line.getTitle();
                }
                String sessionId = line.getSessionId();
                if (!sessionId.equals(lastSession)) {
                    lastSession = sessionId;
                    if (jsonObjdata != null) {
                        jsonObjects.add(jsonObjdata);
                    }
                    jsonObjdata = new JSONObject();
                    n++;
                    jsonObjdata.put("序号", String.valueOf(n));
                    jsonObjdata.put("创建时间", line.getCreated());
                    jsonObjdata.put("ParentTitle", title);
                    jsonObjdata.put("注册渠道", line.getReqType());
                    jsonObjdata.put(line.getName(), line.getValue());
                } else {
                    jsonObjdata = new JSONObject();
                    jsonObjdata.put(line.getName(), line.getValue());
                }
            }
            jsonObjects.add(jsonObjdata);
            result.put("result", jsonObjects.toString());
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }
}
